// subPages/communityService/shop/shop.js
const app = getApp();
let utils = require('../../../utils/util');
var loadMoreView,p = 1,num = 10,type='service'
Page({

  /**
   * 页面的初始数据
   */
  data: {
      selectIndex:0,
      // array:["好评优先","距离优先","人气优先",""],
      supermarketList:[]
  },


    bindPickerChange(e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            selectIndex: e.detail.value
        })
    },
      /**
       * * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        utils.setNavBarColor('#000000','#ffffff');
        utils.setNavBarTitle('商城');
          loadMoreView = this.selectComponent("#loadMoreView")

          this.getShopList();
    },
    getShopList(){
        let _this = this;
        utils.getAjax(`${app.globalData.apiUrl}/get_community_service_shop`,res=>{
            console.log(res);
            if(res.data.list.length != 0){
                var list = [];
                if(p == 1){
                    for(let i in res.data.list){
                        res.data.list[i].start = parseFloat(res.data.list[i].start)
                    }
                    list = res.data.list
                }else{
                    for(let i in res.data.list){
                        res.data.list[i].start = parseFloat(res.data.list[i].start)
                    }
                    list = that.data.supermarketList.concat(res.data.list)
                }
                _this.setData({
                    supermarketList:list
                })
            }else{
                _this.setData({
                    supermarketList:[]
                })
            }
            loadMoreView.loadMoreComplete(res.data,type)
    },{
        type:1,
        p:p,
        num:num,
        community_id:app.globalData.communityId
       })
    },
    //获取服务数据异常
    shopDataErr(err){
        if(p>1){
            loadMoreView.loadMoreFail()
        }else{
            util.showModal('提示','获取商品失败，请向上滑动屏幕')
            this.setData({
                isFail: true
            })
        }
    },
    loadMoreListener: function(e) {
        p += 1
        this.getShopList()
    },

    clickLoadMore: function(e) {
        this.getShopList()
    },
    goToSupermarket(e){
        let shopId = e.currentTarget.dataset.id;
        console.log(shopId);
        wx.navigateTo({
            url:'../shopContent/shopContent?id='+shopId
        })
    },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
      if(this.data.isFail){
          this.getShopList()
          return
      }
      loadMoreView.loadMore()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
